package com.longxi.tianjintianbao.ui.activity.quality;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.model.Personnel;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.presenter.quality.QualityClassListAtPresenter;
import com.longxi.tianjintianbao.ui.view.quality.IQualityClassListAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;

public class QualityClassListActivity extends BaseActivity<IQualityClassListAtView, QualityClassListAtPresenter> implements IQualityClassListAtView {

    private final String TAG = "QualityClassListActivity";

    @BindView(R.id.btnLogin)
    Button mBtnLogin;

    private int page = 0;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_quality_class_list;
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.setBar();
        mPresenter.initAdapter();

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.getQualityClass();
            }

        });
        mPresenter.getQualityClass();
    }

    @Override
    public void initListener() {
        super.initListener();
        //mBtnLogin.setOnClickListener(v -> mPresenter.showQualityStateDialogFragment());
        mBtnLogin.setOnClickListener(v -> mPresenter.goFinshOrChange());
    }

    @Override
    protected QualityClassListAtPresenter createPresenter() {
        return new QualityClassListAtPresenter(this);
    }

    @Override
    public SmartRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    @Override
    public RecyclerView getRvContent() {
        return mRvContent;
    }

    @Override
    public Button getBtnLogin() {
        return mBtnLogin;
    }

    /**
     * 让 dialogfragment获取到父类的人员列表 这样才能进行增加
     * @return
     */
    public List<Personnel> getPersonnels(){
        return mPresenter.getPersonnels();
    }

    /**
     * 用户删除已选择的人
     * @param personnel
     * @return
     */
    public boolean deletePersonnels(Personnel personnel){
        return mPresenter.deletePersonnels(personnel);
    }

    /**
     * 品质检查开始（结束）
     * @param button
     * @param lastdate
     */
    public void saveQualityState(String button, String lastdate){

        if(mPresenter.personnels.size() == 0){
            MyUtils.showToast("请先选择指派人员");
            return;
        }

        if("".equals(lastdate)){
            MyUtils.showToast("请选择最后整改时间");
            return;
        }

        mPresenter.saveQualityState(button,
                mPresenter.personnels.get(0).getUsername(),
                mPresenter.personnels.get(0).getUserid(),
                lastdate);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.onActivityResult(requestCode, resultCode, data);
    }
}
